/*
 * net_rolling_code..c
 *
 *  Created on: 2023年7月22日
 *      Author: zlw
 */
#include "net_rolling_code.h"
#include "device.h"
#include <stdio.h>
static uint32_t itselfRollingCode = 0x00;
#ifdef HUB_DEVICE
static uint32_t recvRollongCode[NODE_MAX_COUNT] = {0x00};
#else
static uint32_t recvRollongCode = 0x00;
#endif

/**
 * biref: 从flash中获取rolling code
 * return:返回获取到的rolling code
 * */
static uint32_t get_deivce_itself_rolling_code(void)
{
  uint32_t rollingCode = 0x00;

  if(getThisParam(ItselfRollingCode, &rollingCode) !=0 )
    setThisParam(ItselfRollingCode, &rollingCode);

  return rollingCode;
}

/**
 * biref:存储发送数据的rolling code
 * note： 将rolling code存储flash接口补充到此函数中
 * */
static void save_deivce_itself_rolling_code(void)
{
  setThisParam(ItselfRollingCode, &itselfRollingCode);
}

/**
 * biref:获取flash中上次接受到的 rolling code
 * param：子设备ID，无则默认填 0x00
 * return:上次的rolling code
 * */
static uint32_t get_device_recv_rolling_code(uint8_t deviceId)
{
  uint32_t rollingCode = 0x00;
#ifdef HUB_DEVICE
  if(deviceId >= 0xFF)
    return 0;
  if(getDeviceParam(deviceId, HubRecvRollingCodeFirst, &rollingCode) != 0)
    setDeviceParam(deviceId, HubRecvRollingCodeFirst, &rollingCode);
#else
  (void)deviceId;
  if(getThisParam(EndDeviceRecvRollingCode, &rollingCode) != 0)
    setThisParam(EndDeviceRecvRollingCode, &rollingCode);
#endif
  return rollingCode;
}

/**
 * biref:将接受到的rolling code进行判断，再存flash
 * param: 子设备ID，无则默认填0x00
 * */
void save_device_recv_rolling_code(uint8_t deviceId, uint32_t rollingCode)
{
#ifdef HUB_DEVICE
  if(deviceId >= 0xFF)
    return ;
  recvRollongCode[deviceId] = rollingCode;
  if(recvRollongCode[deviceId] % SAVE_ROLLING_CODE_COUNT == 0)
    setDeviceParam(deviceId,  HubRecvRollingCodeFirst, &recvRollongCode[deviceId]);
#else
  (void)deviceId;
  recvRollongCode = rollingCode;
  if(recvRollongCode % SAVE_ROLLING_CODE_COUNT == 0)
    setThisParam(EndDeviceRecvRollingCode, &recvRollongCode);
#endif
}

/**
 * @biref: 设备上电或者配网成功后，设备自身rollingcode初始化
 * @param: 1:初始化类型； 2：rolling code 初始化类型为上电时 rolling code默认为 0
 * @note: 设备上电后将掉电前的rolling code 加SAVE_ROLLING_CODE_COUNT
 * 防止出现rolling code变小的情况
 * */
void itself_rolling_code_init(rollingCodeInit_t initStatus, uint32_t rollingCode)
{
  if(START_UP_INIT == initStatus) {
      itselfRollingCode = get_deivce_itself_rolling_code() + SAVE_ROLLING_CODE_COUNT;
  }
  else if(PROVINED_INIT == initStatus) {
      itselfRollingCode = rollingCode;
      save_deivce_itself_rolling_code();
  }
}

/**
 * @brief:设备上电或配网后，设备接收对方的rolling code初始化；
 * @param: 1:初始化类型； 2device id; 2：rolling code
 * @note: 初始化类型为上电时 rolling code 参数默认为 0；
 * 设备类型为RFM_DEVICE时 deviceId 默认参数为0
 * */
void recv_rolling_code_init(rollingCodeInit_t initStatus, uint8_t deviceId, uint32_t rollingCode)
{
  if(START_UP_INIT == initStatus) {
#ifdef HUB_DEVICE
      if(deviceId >= 0xFF)
        return ;
      recvRollongCode[deviceId] = get_device_recv_rolling_code(deviceId);
#else
      recvRollongCode = get_device_recv_rolling_code(deviceId);
#endif
  }
  else if(PROVINED_INIT == initStatus) {
#ifdef HUB_DEVICE
      if(deviceId >= 0xFF)
        return ;
      recvRollongCode[deviceId] = rollingCode;
#else
      recvRollongCode = rollingCode;
#endif
      save_device_recv_rolling_code(deviceId, rollingCode);
  }
}

/**
 * param： 1:expander设备需要输入子设备ID，其他设备默认输入 0x00; 2:收到的数据帧里的rolling code
 * biref: 判断接收到的数据帧里的rolling code
 * return: 返回判断结果
 * **/
rollingCode_t judge_revice_rolling_code(uint8_t deviceId, uint32_t rollingCode)
{
  uint32_t oldRollingCode = 0x00;
  rollingCode_t judgeStatus = 0x00;

#ifdef HUB_DEVICE
  if(deviceId >= 0xFF)
    return RPLLING_CODE_LESS;
  oldRollingCode = recvRollongCode[deviceId];
#else
  (void)deviceId;
  oldRollingCode = recvRollongCode;
#endif

  if(rollingCode > oldRollingCode || rollingCode == 0xFFFFFFFF)
      judgeStatus = ROLLING_CODE_MORE;
  else if(rollingCode < oldRollingCode)
      judgeStatus = RPLLING_CODE_LESS;
  else
    judgeStatus = ROLLING_CODE_EQUAL;

  if (judgeStatus != ROLLING_CODE_MORE)
  {
    printf("OldRC:%08lX ",oldRollingCode);//RC not mastch
  }
  return judgeStatus;
}

/**
 * biref: 获取发送数据帧中的rolling code
 * return: 返回rolling code
 * note： 每当rolling code累计增加到 SAVE_ROLLING_CODE_COUNT 时存储至flash中
 **/
uint32_t get_device_itself_rolling_code(void)
{
  uint32_t rollingCode = itselfRollingCode;

  if(itselfRollingCode % SAVE_ROLLING_CODE_COUNT == 0) {
      save_deivce_itself_rolling_code();
  }
  itselfRollingCode ++;

  return rollingCode;
}
